package net.togogo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloContextServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取web.xml配置中的上下文参数
        ServletContext context = getServletConfig().getServletContext();
        String username = context.getInitParameter("username");
        System.out.println("获取web.xml配置中的上下文参数:username---->" + username);
        System.out.println("获取web.xml配置中的上下文参数:password---->" + context.getInitParameter("password"));
        //获取当前工程的路径
        System.out.println("获取当前工程的路径--->" + context.getContextPath());
        //获取工程部署后在磁盘上的绝对路径
        System.out.println("获取工程部署后在磁盘上的绝对路径--->" + context.getRealPath("/img/1.jpg"));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
